const koa = require("koa2");
const Router = require("koa-router");
const SenecaWeb = require("seneca-web");

const route = require("./router");
const seneca = require("./seneca");
const envPath = "../env.yaml"
const config = require("./config/env")
config(envPath)


const app = new koa();


try {
    seneca.use(SenecaWeb, {
        context: Router(),
        adapter: require("seneca-web-adapter-koa2"),
        routes: route,
        options: { parseBody: false },
    });
    seneca.listen({
        type: "tcp",
        port: process.env['SENECA-SERVICE-PORT'],
        pin: "server:blog-seneca-service",
        host: process.env['SENECA-SERVICE-HOST']
    })
    seneca.ready(() => {
        app.use(seneca.export("web/context")().routes());
        app.listen({
            port: process.env['KOA-SENECA-SERVICE-POST'],
            host: process.env['KOA-SENECA-SERVICE-HOST']
        });
    });
} catch (error) {
    console.log("src/index:" + error);
}